import { app } from "../../base";
import { Link } from "../../app_base/module/structure/Link";
import { G_Config } from "../../config/ConfigManager";
import { Levels } from "../../config/ConfigStructures";
import { CheckInLink } from "../links/CheckInLink";
import { LevelLink } from "../links/LevelLink";
import { ConditionLink } from "../links/ConditionLink";
import { G_Data } from "../DataCenter";

type MeStruct = {
   /**
    * 角色ID
    */
   id: string;
   /**
    * 角色昵称
    */
   nick?: string;
}


/**
 * 当前玩家数据</br>
 * @event G_Data.me.onUpdated 监听玩家所有数据更新
 * @event G_Data.me.coin.onUpdated 监听玩家代币数据更新
 * @event G_Data.me.energy.onUpdated 监听玩家体力数据更新
 */
export class MeVo extends Link<MeStruct>{
   private static created: boolean = false;
   private static ME_INFO_KEY = "MeVo";
   constructor() {
      if (MeVo.created) {
         throw new Error("MeVo 为单例");
      }
      super("me", {
         id: "",
         nick: ""
      });
      //游戏开发者，根据游戏业务需求加减数据模块，【数据自动写入本地缓存，无需手动维护】
      //已经支持部分数据：关卡(LevelLink)、代币（Coin）、体力(Energy)、签到(CheckIn)
      // this.append(LevelLink, CoinLink, EnergyLink, CheckInLink);
      MeVo.created = true;
   }

   /**
    * 初始化当前角色数据
    * @param id 平台登陆的id
    * @param nick 平台昵称
    * @returns 
    */
   public init(id: string, nick?: string): MeVo {
      this._data.id = id;
      this._data.nick = nick || id;
      let data = app.storage.getUValue<object>(MeVo.ME_INFO_KEY, {});
      if (!data || Object.keys(data).length == 0) {
         this._initNewUserData();
      } else {
         this.parse(data);
      }

      // //初始化体力系统
      // this.energy && this.energy.init();
      // this._lastLoginTime = App.storage.getUValue(MeVo.LAST_LOGIN_TIME_KEY, G_Data.server.time);

      this.offUpdated(this, this._savePlayerInfo);
      this.onUpdated(this, this._savePlayerInfo);

      this._savePlayerInfo();

      
      return this;
   }

   private _initNewUserData(): void {
      //默认解锁第一个关卡
      let firstLevel: Levels = G_Config.level.findData(0);
      firstLevel && this.level.unlockLevel(firstLevel.id);
   }

   /**
    * 关卡数据
    */
   public get level(): LevelLink {
      return this.child("level");
   }

   /**
    * 签到数据
    */
   public get checkIn(): CheckInLink {
      return this["checkIn"] || this.child("checkIn");
   }

   /**
    * 上次登录时间
    */
   // public get lastLoginTime(): number {
   //    return app.storage.lastLoginTime;
   // }

   /**
 * 条件数据
 */
   public get condition(): ConditionLink {
      return this.child("condition");
   }

   /**
    * 是否为新玩家
    */
   public get isNewer(): boolean {
      return app.App.isFirstLaunchOfThisUser;
   }

   public get id(): string {
      return this._data.id;
   }

   public get nick(): string {
      return this._data.nick;
   }

   protected _restSelfData(): void {
      this._data.id = "";
      this._data.nick = "";
   }

   /**保存存档 */
   private _savePlayerInfo() {
      app.storage.setUValue(MeVo.ME_INFO_KEY, this.toObject());
   }
}